package c;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-29
 * Time: 15:24
 */
//测试类
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入元素个数:");
        int count = scanner.nextInt();
        System.out.println("请输入要查找的序号: ");
        int k = scanner.nextInt();
        TestHeap testHeap =new TestHeap(k);
        System.out.println("请输入元素结合:");
        int[] array = new int[count];
        for (int i = 0; i < count; i++) {
            array[i] = scanner.nextInt();
        }
        //构建元素个数为k 的小根堆
        for (int i = 0; i < k; i++) {
            testHeap.offer(array[i]);
        }
        //确保小根堆中的元素是数据集合中的最大的
        for (int i = k; i < array.length; i++) {
            int tmp = array[i];
            if(tmp > testHeap.peek()){
                testHeap.poll();
                testHeap.offer(tmp);
            }
        }

        int sum = 0;

        for (int i = 0; i < k; i++) {
            sum += testHeap.poll();
        }
        System.out.println(sum);

    }
}
